home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / piechart / piechart.awk < prev    next >
Text File  |  1992-10-25  |  7KB  |  174 lines

  1. #! /bin/awk
  2.  
  3. #                               -*- Mode: Awk -*- 
  4. # piecharts.awk --- Creation of pie charts in PostScript, via LaTeX and
  5. PSTricks
  6. # Author          : Denis GIROU (CNRS/CIRCE - France) <girou@circe.fr>
  7. # Created the     : Fri Oct 16 21:53:41 1992
  8. # Last mod. by    : Denis GIROU (CNRS/CIRCE - France) <girou@circe.fr>
  9. # Last mod. the   : Tue Oct 20 20:03:39 1992
  10. # Version         : 1.0
  11. #
  12. # Description     :   piecharts.awk is a simple AWK program to realize hight
  13. #                   quality, greatly customizable, grayscaled or colored pie
  14. #                   charts in PostScript.
  15. #                     It uses LaTeX and the splendid pstricks package of
  16. #                   Timothy Van Zandt <tvz@princeton.edu>, available in
  17. #                   /anonymous@princeton.edu:/pub
  18. #                     It creates the LaTeX (+ PSTricks) code for direct
  19. #                   insertion in a LaTeX document of the pie chart generated.
  20. #                   It use a data file which describe the parts and the labels
  21. #                   for them.
  22. #                     You always can modify the generated code for more
  23. #                   sophisticated result.
  24. #
  25. # Syntax          : awk -f piecharts.awk [-v SCALE=scale] [-v BOXIT=boxit]
  26. #                                                     <data_file >output_file
  27. #                     Default scale factor is 1.
  28. #                     Use BOXIT=boxit if you want a box around the figure.
  29. #
  30. # Input file      :   The fill styles are: none, solid, vlines, vlines*,
  31. #                   hlines, hlines*, crosshatch, crosshatch*
  32. #                     The predefined colors are : black, darkgray, gray,
  33. #                   lightgray, white, red, green, blue, cyan, magenta, yellow
  34. #                     You can easily define new gray or colors, or access to a
  35. #                   rgb palette with the palette.sty file of PSTricks.
  36. #                     You can also change a lot of parameters. See the
  37. #                   documentation of PSTricks for more customization.
  38. #
  39. # Input file format:  You can add blanks lines and comments (beginning by #).
  40. #                     The default field separator is | You can change it at
  41. #                   the awk level.
  42. #                     The first non-comment line has 3 fields for titles, and
  43. #                   after you must have one line by part of the pie chart,
  44. #                   with 5 fields each (percentage, inside label, outside
  45. #                   label, filling, color).
  46. #                     Evidentely, the total of the percentages must be 360, or
  47. #                   less. 
  48. #                     You can add LaTeX commands in the labels (\em, \small,
  49. #                   etc.).
  50. #                     Be careful of some LaTeX special characters (see $ and %
  51. #                   in the examples).
  52. #                     Here are two examples (suppress the first 4 characters
  53. #                   of each line):
  54. #
  55. #   # Data for the first example of pie charts
  56. #   
  57. #   # Title of the figure      | Title for the eventual table of figures
  58. | Label
  59. #   \bf Example 1 of pie chart | Pie Chart 1                            
  60. | piechart1
  61. #   
  62. #   # Value | Inside label   | Outside label | Type of filling | Color
  63. of filling
  64. #        70 | \small\$9.0M   | {\em Paris}   | hlines          | lightgray
  65. #       130 | \small\$16.70M | {\em London}  | vlines          | gray
  66. #       160 | \small\$23.1M  | {\em Berlin}  | crosshatch      | black
  67. #   
  68. #   
  69. #   # Data for the second example of pie charts
  70. #   
  71. #   # Title of the figure      | Title for the eventual table of figures
  72. | Label
  73. #   \bf Example 2 of pie chart | Pie Chart 2                            
  74. | piechart2
  75. #   
  76. #   # Value | Inside label | Outside label | Type of filling | Color of filling
  77. #       16  |              |  16\%         | solid           | red
  78. #      113  |              | 113\%         | solid           | blue
  79. #        6  |              |   6\%         | solid           | green
  80. #      149  |              | 149\%         | solid           | magenta
  81. #       76  |              |  76\%         | solid           | cyan
  82. #
  83. #
  84. # Examples        :   If you name these files piechart1.dat and piechart2.dat,
  85. #                   you can simply type:
  86. #   awk -f piecharts.awk -v SCALE=1.5 <piechart1.dat >piechart1.tmp
  87. #   awk -f piecharts.awk -v BOXIT=boxit -v SCALE=0.7 <piechart2.dat
  88. >piechart2.tmp
  89. #                   and just insert the two result files in a LaTeX document
  90. #                   at the right place...
  91. #
  92. # Portability     :   Expected great. Tested on an RS6000 under AIX 3.2.
  93. #
  94. # Known problems  :   Evidentely, take care of the labels for the small parts.
  95. #                   If they are too long or in a too big font, the result will
  96. #                   not be pretty...
  97.  
  98.  
  99. # Beginning of the AWK program
  100. BEGIN{
  101.      FS="|"                              # Fields separator
  102.      PARAM="NO"
  103.      BEGIN_VALUE=0
  104.      TOTAL=0
  105.      if (SCALE == "")
  106.         SCALE=1.
  107.      print "\\begin{figure}[htbp]"
  108.      # Framebox around the figure, or not
  109.      if (BOXIT == "boxit" || BOXIT == "BOXIT" )
  110.         print "  \\psframebox{"
  111.      print "  \\begin{minipage}{\\textwidth}"
  112.      print "    \\begin{center}"
  113.      if (BOXIT == "boxit" || BOXIT == "BOXIT" )
  114.         print "      \\vspace{5mm}"
  115.      print "      \\psset{unit=" SCALE "}"
  116.      print "      \\begin{pspicture}(-2,-2)(2,2)"
  117.      print "        \\Polar"
  118.      print "        \\psset{framesep=1.5pt}"
  119. }
  120.  
  121. {
  122.  # Comments and blanks lines
  123.  if (substr($1,1,1) == "#" || $1 == "")
  124.     break
  125.  
  126.  # Suppression of the right and left blanks of the variables
  127.  for (i=1;i<=NF;i++) {
  128.     TEMP = sub(/^ */,"",$i)
  129.     TEMP = sub(/ *$/,"",$i)}
  130.  
  131.  if (PARAM == "NO") {
  132.  # Generic parameters : title, title for the eventual table of figures, label
  133.     PARAM="YES"
  134.     TITLE=$1
  135.     TABLE_TITLE=$2
  136.     LABEL=$3}
  137.    else {
  138.     TOTAL=TOTAL+$1
  139.     print "        \\pswedge[fillstyle=" $4 ",fillcolor=" $5 "]{2}{"
  140. BEGIN_VALUE "}{" TOTAL "}"
  141.     if (TOTAL > 360) {
  142.        print "Error ! Total of percentages greater than 360! (" TOTAL ")"
  143.        exit}
  144.     VAL=BEGIN_VALUE+($1-val_pourc)/2
  145.     # Inside label
  146.     if ($2 != "")
  147.        print "        \\rput(1.2," VAL ") {\\psframebox*{" $2 "}}"
  148.     # Outside label
  149.     if ($3 != "") {
  150.        if (VAL < 91)
  151.           POSITION="bl"
  152.           else if (VAL < 181)
  153.            POSITION="br"
  154.            else if (VAL < 271)
  155.             POSITION="tr"
  156.             else POSITION="tl"                         
  157.        print "        \\rput[" POSITION "](2.2," VAL ") {" $3 "}"}
  158.     BEGIN_VALUE=TOTAL}
  159. }
  160.  
  161. END{
  162.     print "      \\end{pspicture}"
  163.     print "    \\end{center}"
  164.     print "  \\caption[" TABLE_TITLE "]{" TITLE "}"
  165.     print "  \\label{f:" LABEL "}"
  166.     print "  \\end{minipage}"
  167.     if (BOXIT == "boxit" || BOXIT == "BOXIT" )
  168.       print "  }"
  169.     print "\\end{figure}"
  170. }
  171.  
  172.  
  173.  
  174.